<?php
class Article_m extends MY_Model{
	
	private $_table_lang = 'news_article_lang';
	
	private $select = 'a.id, a.created,a.image, a.cat_id, l.title, l.slug,l.keywords,l.description';
	private $select_full = 'a.id, a.created,a.image, a.cat_id, l.title, l.keywords,l.description, l.slug,l.bodyhtml';
	
	function __construct(){
		parent::__construct();
		$this->_table = 'news_article';
		
	}
	
	public function get_all(){
		$this->_table .= ' a';
		$this->_table_lang .= ' l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang, 'l.id = a.id ');
		$this->db->where('l.lang',$this->_lang);
		return $this->db->get($this->_table)->result();
	}
	
	public function get( $id ){
		$this->_table .= ' a';
		$this->_table_lang .= ' l';
		$this->db->select($this->select_full);
		$this->db->join($this->_table_lang, 'l.id = a.id ');
		$this->db->where('l.lang',$this->_lang);
		$this->db->where('a.id',$id);
		return $this->db->get($this->_table)->row();
	}
	
	public function get_many_by($params = array(),$page_size = 10)
	{
		$this->_table .= ' a';
		$this->_table_lang .= ' l';
		//$page_size = 10;
		
		
		$this->db->select($this->select);
		$this->db->from($this->_table);
		$this->db->join($this->_table_lang, 'l.id = a.id ');
		$this->db->where('l.lang',$this->_lang);
		if ( ! empty($params['cat_id']))
		{
			$this->db->where('a.cat_id',$params['cat_id']);
		}

//		$this->db->get($this->_table);
		
		$total_row = $this->db->count_all_results();//$this->get_total_row();
		//echo $this->db->last_query();
		//echo $total_row;exit;

		
		//step 2
		$this->db->select($this->select);
		$this->db->order_by('a.created','desc');
		$this->db->join($this->_table_lang, 'l.id = a.id ');
		$this->db->where('l.lang',$this->_lang);
		if ( ! empty($params['cat_id']))
		{
			$this->db->where('a.cat_id',$params['cat_id']);
		}
		if ( ! empty($params['limit']))
		{
			 $this->db->limit($page_size,$params['limit'] * $page_size);			
		}
		else{
			$this->db->limit($page_size);		
		}
		
		$articles = $this->db->get($this->_table)->result();

		
		
		$model = new stdClass();
		//data
		$model->articles = $articles;
		$model->total_row = $total_row;
		return $model;
	}
	
	public function insert($data){
		//print_r($data);exit;
		//step 1
		$article['created'] = strtotime(date('Y-m-d H:i'));
		$article['cat_id'] = $data['cat_id'] ? $data['cat_id'] : 0;
		$article['create_by'] = $this->current_user->id;
		$article['image'] = $data['image'] ? $data['image'] : "";
		$id_insert = parent::insert($article);
		//step2
		$this->_table = $this->_table_lang;
		if(isset($data['slug'])){
			$check = $this->db->where('slug',$data['slug'],'lang',$this->_lang)->get($this->_table)->row();
			if(count($check)>0)
				$data['slug'] .= "-".time();
		}
		else{
			$data['slug'] .= "-".time();
		}
		//insert multilang
		$lang_data['id'] = $id_insert;
		$lang_data['title'] = $data['title'];
		$lang_data['slug'] = $data['slug'];
		$lang_data['keywords'] = $data['keywords'];
		$lang_data['description'] = $data['description'];
		
		$lang_data['bodyhtml'] = $data['bodyhtml'];
		foreach($this->_lang_data as $l){
			$lang_data['lang'] = $l;
			parent::insert($lang_data);
		}
		return $id_insert;
	}
	
	public function update($id,$data){
		//update news_article
		$this->_table = 'news_article';
		if(isset($data['image']))
			$article['image'] = $data['image'];
		$article['cat_id'] = $data['cat_id'];
		parent::update($id,$article);
		//update news_article_lang
		
		
		//step2
		$this->_table = 'news_article_lang';
		if(isset($data['slug'])){
			$check = $this->db->where_not_in('id', $id)->where('slug',$data['slug'])->get($this->_table)->row();
			if(count($check)>0)
				$data['slug'] .= "-".time();
		}
		else{
			$data['slug'] .= "-".time();
		}
		$data_lang['title'] = $data['title'];
		$data_lang['slug'] = $data['slug'];
		$data_lang['bodyhtml'] = $data['bodyhtml'];
		$data_lang['keywords'] = $data['keywords'];
		$data_lang['description'] = $data['description'];
		$this->db->where('id',$id);
		$this->db->where('lang',$this->_lang);
		parent::update($id,$data_lang);
	}
	
	
	function get_total_row(){
		$result = 0;
		$q = $this->db->last_query();
		$result = count($this->db->query($q)->result());
//		echo $this->db->last_query();
		return $result;
	}
	
	function delete( $id ){
		//step1
		//xoa hinh anh truoc
		$path_news = "./uploads/news/";
		$image = $this->get($id)->image;
		if(is_file($path_news.$image))
			unlink($path_news.$image);
		
		$this->_table = 'news_article';
		parent::delete($id);
		$this->_table = 'news_article_lang';
		parent::delete($id);
	}
	
	//news/categories return categories , articles
	function categories ( $slug , $page_size = 5, $page_index = 0 ){
		$model = new stdClass();
		$cat = 'news_categories c';
		$cat_lang = 'news_categories_lang l';
		
		$this->db->select('c.id,l.title,l.slug');
		$this->db->join($cat_lang, 'l.id = c.id');
		$this->db->where('l.lang',$this->_lang);
		$this->db->where('l.slug',$slug);
		$cat_data = $this->db->get($cat)->row();
		//print_r($cats);exit;
		//articles
		
		$this->_table = 'news_article a';
		$this->_table_lang = 'news_article_lang l';
		$this->db->select($this->select);
		$this->db->from($this->_table);
		$this->db->join($this->_table_lang, 'l.id = a.id ');
		$this->db->where('a.cat_id',$cat_data->id);
		$this->db->where('l.lang',$this->_lang);
		$total_row = $this->db->count_all_results();//$this->get_total_row();
		//total row
		
		$this->_table = 'news_article a';
		$this->_table_lang = 'news_article_lang l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang, 'l.id = a.id ');
		$this->db->where('a.cat_id',$cat_data->id);
		$this->db->where('l.lang',$this->_lang);
		
		//$this->db->limit($page_size,$page_index * $page_size);12345678
		//echo $page_index;exit;
		$this->db->limit($page_size, $page_index * $page_size);
		
		$articles = $this->db->get($this->_table)->result();
		
		
		$model->categories = $cat_data;
		$model->articles = $articles;
		$model->total_row = $total_row;
		return $model;
	}
	
	function detail( $slug ){
		$this->_table .= ' a';
		$this->_table_lang .= ' l';
		$this->db->select($this->select_full);
		$this->db->join($this->_table_lang, 'l.id = a.id ');
		$this->db->where('l.slug',$slug);
		$this->db->where('l.lang',$this->_lang);
		return $this->db->get($this->_table)->row();
	}
	
}